# coding=utf8

import collections

def get_train_data_list():
    global lines
    d = collections.defaultdict(list)
    for line in lines:
        pieces = line.split('\t:\t')
        d[pieces[1]].append(line)
    return d

lines = open('../data/categorySentenceResultSortedCateFilter.txt').readlines()
test_n_write = open('../data/test_n.txt', 'w')
types_list = open('../data/intermediate/negative_example.txt').readlines()
ratio = 10
d = get_train_data_list()
print 'load data complete'
for types in types_list:
    type_list = types.split('\t:\t')
    src_type = type_list[0]

    src_num = len(d[src_type])
    cnt = 0
    for i in xrange(1, len(type_list)):
        if cnt > ratio*src_num:
            break
        train_data_list = d[type_list[i]]
        for train_data in train_data_list:
            if cnt > ratio*src_num:
                break
            test_n_write.write(src_type+'\t:\t'+train_data)
            cnt += 1

test_n_write.close()
print 'generate negative example complete'
